package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class MessageBoardDetailAdapter extends BaseAdapter {

    private Context mContext;
    private String branch_id;
    private ArrayList<LinkedTreeMap> mDataList;

    public MessageBoardDetailAdapter(Context mContext, String branch_id) {
        this.mContext = mContext;
        this.branch_id = branch_id;
    }

    public void setData(ArrayList<LinkedTreeMap> mDataList){
        this.mDataList = mDataList;
    }

    public ArrayList<LinkedTreeMap> getData(){
        return mDataList;
    }

    public void loadMoreListView(ArrayList<LinkedTreeMap> dataList) {
        if(mDataList==null)
            mDataList=new ArrayList<>();
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(mDataList == null){
            mDataList = new ArrayList<LinkedTreeMap>();
        }
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public void updateListView(ArrayList<LinkedTreeMap> dataList) {
        this.mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (mDataList.size() == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_message_board_detail_item, parent, false);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.zw=(TextView) convertView.findViewById(R.id.zw);
                holder.time=(TextView) convertView.findViewById(R.id.time);
                holder.content=(TextView) convertView.findViewById(R.id.content);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (mDataList.size() == 0) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.name.setText(mDataList.get(position).get("user_name")+"");
            holder.time.setText(mDataList.get(position).get("book_date")+"");
            holder.content.setText(mDataList.get(position).get("book_content")+"");
//            holder.zw.setText(""+mDataList.get(position).get("book_sort"));
        }

        return convertView;
    }

    private static final class ViewHolder {
        TextView name, zw, time, content;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
